
Page( {  

  data: {  
    imgs: [],
    count: 3,
    tabArr: {  
      curHdIndex: 0,  
      curBdIndex: 0  
    },  
  },  
  tabFun: function(e){  
    //获取触发事件组件的dataset属性  
    var _datasetId=e.target.dataset.id;  
    var _obj={};  
    _obj.curHdIndex=_datasetId;  
    _obj.curBdIndex=_datasetId;  
    this.setData({  
      tabArr: _obj  
    });  
  },  
  onLoad: function( options ) {  
    console.info(options)
  },
  bindUpload: function (e) {
    switch (this.data.imgs.length) {
      case 0:
        this.data.count = 4
        break
      case 1:
        this.data.count = 4
        break
      case 2:
        this.data.count = 4
        break
    }
    var that = this
    wx.chooseImage({
      count: that.data.count, // 默认3
      sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0], //选择图片返回的相对路径
          encoding: 'base64', //编码格式
          success: res => { //成功的回调
            console.log('data:image/png;base64,' + res.data)  //输出base64
            that.data.imgs.push('data:image/png;base64,' + res.data)
            that.setData({
              imgs: that.data.imgs
            })
          }
        })
   


        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        // for (var i = 0; i < tempFilePaths.length; i++) {
        //   wx.uploadFile({
        //     url: wx.baseUri + '/upload_sx',
        //     filePath: tempFilePaths[i],
        //     name: "file",
        //     header: {
        //       "content-type": "multipart/form-data"
        //     },
        //     success: function (res) {
        //       debugger
        //       if (res.statusCode == 200) {
        //         wx.showToast({
        //           title: "上传成功",
        //           icon: "none",
        //           duration: 1500
        //         })   
        //         that.data.imgs.push(JSON.parse(res.data).data)   
        //         that.setData({
        //           imgs: that.data.imgs
        //         })
        //       }
        //     },
        //     fail: function (err) {
        //       wx.showToast({
        //         title: "上传失败",
        //         icon: "none",
        //         duration: 2000
        //       })
        //     },
        //     complete: function (result) {
        //       console.log(result.errMsg)
        //     }
        //   })
        // }
      }
    })
  },
  // 删除图片
  deleteImg: function (e) {
    var that = this
    wx.showModal({
      title: "提示",
      content: "是否删除",
      success: function (res) {
        if (res.confirm) {
          for (var i = 0; i < that.data.imgs.length; i++) {
            if (i == e.currentTarget.dataset.index) that.data.imgs.splice(i, 1)
          }
          that.setData({
            imgs: that.data.imgs
          })
        } else if (res.cancel) {
          console.log("用户点击取消")
        }
      }
    })
  },

  previewImg:function(e){
    console.log(e.currentTarget.dataset.index);
    var index = e.currentTarget.dataset.index;
    var imgArr = this.data.imgs;
    wx.previewImage({
      current: imgArr[index],     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  }
});